package com.foreveross.bsl.mam.app.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.bsl.mam.app.application.AppSecretApplication;
import com.foreveross.bsl.mam.app.application.vo.AppSecretVo;

/**
 * 应用打包证书管理
 * 
 * @author panhanmao
 * @2013-6-22 @下午3:01:42
 */
@Controller
@RequestMapping("/mam/secret")
public class AppSecretController {

	@Inject
	private AppSecretApplication appSecretApplication;


	/**
	 * 进入上传证书页面
	 * 
	 * @param appId
	 * @param platform
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{platform}", method = RequestMethod.GET)
	public String enterCert(@RequestParam("appId") String appId,
			@PathVariable("platform") String platform, Model model) {
		model.addAttribute("appId", appId);
		model.addAttribute("platform", platform);
		return "app/packCert";
	}

	/**
	 * 保存证书
	 * 
	 * @param appId
	 * @param platform
	 * @param appPlatformVo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{platform}", method = RequestMethod.POST)
	public Map<String, String> add(@RequestParam("appId") String appId,
			@PathVariable("platform") String platform, AppSecretVo appSecretVo) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			AppSecretVo secretVo = appSecretApplication.findByAppId(appId,
					platform);
			if (secretVo != null ) {
				appSecretVo.setId(secretVo.getId());
				appSecretApplication.updateEntity(appSecretVo);
			} else {
				appSecretApplication.saveEntity(appSecretVo);
			}
			result.put("result", "success");
		} catch (Exception e) {
			result.clear();
			result.put("result", "error");
		}
		return result;
	}

}
